package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.drm.DrmStore;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class CoachActivity extends Activity implements AdapterView.OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coach);

        ListView caochListview = (ListView) findViewById(R.id.coach_list_view);
        CoachAdapter coachAdapter = new CoachAdapter(this, getData());
        caochListview.setAdapter(coachAdapter);
        caochListview.setOnItemClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private List<Map<String, Object>> getData() {

        List<Map<String, Object>> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", "asdfasd");
            map.put("dage", 9);
            map.put("student", "200");
            map.put("evaluate", "99%");
            list.add(map);
        }

        return list;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent goInfo = new Intent(this, CoachInfoActivity.class);
        this.startActivity(goInfo);
    }


    class CoachAdapter extends BaseAdapter {

        private LayoutInflater layoutInflater;
        private List<Map<String, Object>> data;

        public CoachAdapter(Context context, List<Map<String, Object>> list) {
            this.layoutInflater = LayoutInflater.from(context);
            this.data = list;
        }

        @Override
        public int getCount() {
            return this.data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = this.layoutInflater.inflate(R.layout.coach_list_item, null);
                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.coach_item_name);
                holder.age = (TextView) convertView.findViewById(R.id.coach_item_age);
                holder.student = (TextView) convertView.findViewById(R.id.coach_item_student);
                holder.evaluate = (TextView) convertView.findViewById(R.id.coach_item_evaluate);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.name.setText(data.get(position).get("name").toString());
            holder.age.setText(MessageFormat.format(getResources().getString(R.string.coach_item_jiaoling), data.get(position).get("dage").toString()));
            holder.student.setText(MessageFormat.format(getResources().getString(R.string.coach_item_student), data.get(position).get("student").toString()));
            holder.evaluate.setText(MessageFormat.format(getResources().getString(R.string.coach_item_evaluate), data.get(position).get("evaluate").toString()));

            return convertView;
        }
    }

    public final class ViewHolder {
        public TextView name;
        public TextView age;
        public TextView student;
        public TextView evaluate;
    }
}
